<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 10/13/14
 * Time: 3:45 PM
 */
namespace BC\Games\Holdem\Hands;

use BC\Games\Holdem\Cards;
use BC\Games\Holdem\Deck;
use CPath\UnitTest\ITestable;
use CPath\UnitTest\IUnitTestRequest;

class FourOfAKindHand implements IPokerHand, ITestable
{
	const RANK = 8000;

	private $mFourCards;
	private $mKicker;
	public function __construct(Array $fourCards, $kicker) {
		$this->mFourCards = $fourCards;
		$this->mKicker = $kicker;
	}

	/**
	 * Return name of this hand
	 * @return String
	 */
	function getName() {
		return "Four of a Kind";
	}

	function getCards($withNullDelimiter=false) {
		$cards = array_merge(
			$this->mFourCards,
			array(null),
			$this->mKicker
		);
		return $withNullDelimiter ? $cards : array_filter($cards);
	}

	/**
	 * Get Hand rank
	 * @return int
	 */
	function getRank() {
		return self::RANK
			+ ($this->mFourCards[0] & Cards::NUMBER_MASK)
			+ ($this->mKicker & Cards::NUMBER_MASK)/100;
	}

	/**
	 * Compare two hands
	 * @param IPokerHand $Hand
	 * @return int 0, <0, or >0
	 */
	function compare(IPokerHand $Hand) {
		$rank = $this->getRank() - $Hand->getRank();
		if(!$Hand instanceof FourOfAKindHand)
			return $rank;

		$rank = $this->mFourCards[0] - $Hand->mFourCards[0];
		if($rank !== 0)
			return $rank;

		$rank = $this->mKicker - $Hand->mKicker;
		return $rank;
	}

	/**
	 * Returns a string representation of this poker hand
	 * @return String
	 */
	function __toString() {
		return
			"[" . Cards::printCards($this->mFourCards[0])
			. ", " . Cards::printCards($this->mFourCards[1])
			. ", " . Cards::printCards($this->mFourCards[2])
			. ", " . Cards::printCards($this->mFourCards[3])
			. "], " . Cards::printCards($this->mKicker);
	}

	// Static


	/**
	 * Tests a poker hand to see if it matches an array of cards
	 * @param array $cards
	 * @param IPokerHand[] $Matches
	 * @return bool true if the set of cards matches this poker hand
	 */
	static function match(Array $cards, &$Matches = array()) {
		$found = false;

		$matches = array();
		foreach ($cards as $card) {
			$num = $card & Cards::NUMBER_MASK;
			if (isset($matches[$num])) {
				$matches[$num][] = $card;
				if (sizeof($matches[$num]) === 4) {
					$kickers = array_diff($cards, $matches[$num]);
					rsort($kickers);
					$kicker = $kickers[0];

					$Matches[] = new FourOfAKindHand($matches[$num], $kicker);
					$found = true;
				}

			} else {
				$matches[$num] = array($card);
			}
		}

		return $found;
	}

	// Static

	/**
	 * Perform a unit test
	 * @param IUnitTestRequest $Test the unit test request inst for this test session
	 * @return void
	 * @test --disable 0
	 * Note: Use doctag 'test' with '--disable 1' to have this ITestable class skipped during a build
	 */
	static function handleStaticUnitTest(IUnitTestRequest $Test) {
		$Deck = new Deck();
		$Deck->replay(Cards::ACE | Cards::HEARTS, 'player1');
		$Deck->replay(Cards::FIVE | Cards::CLUBS, 'player1');
		$Deck->replay(Cards::ACE | Cards::CLUBS, 'board');
		$Deck->replay(Cards::SEVEN | Cards::HEARTS, 'board');
		$Deck->replay(Cards::ACE | Cards::SPADES, 'board');
		$Deck->replay(Cards::TWO | Cards::CLUBS, 'board');
		$Deck->replay(Cards::ACE | Cards::DIAMONDS, 'board');

		$cards = $Deck->getDrawnCards('player1', 'board');
		if(!FourOfAKindHand::match($cards, $Hands))
			$Test->fail("Four of a kind not matched to cards: " . Cards::printCards($cards));

		$Test->log("Matched Four of a Kind to " . $Hands[0] . ' from ' . Cards::printCards($cards), $Test::VERBOSE);
	}
}